package com.jinlingxiu.app.mvp.presenter

import com.blankj.utilcode.util.Utils
import com.jinlingxiu.app.mvp.contract.LoginContract
import com.jinlingxiu.app.other.db.database.DatabaseManager
import com.jinlingxiu.app.other.db.entity.UserInfoBean
import com.xdja.easymvp.mvp.BasePresenter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 描述:
 * Create by yuanwanli
 * Date 2020/09/09
 */
class LoginPresenter(model: LoginContract.Model, rootView: LoginContract.View) :
    BasePresenter<LoginContract.Model, LoginContract.View>(model, rootView) {
    fun login(name: String, unitName: String) {
        launch {
            withContext(Dispatchers.IO) {
                val userInfoBean = UserInfoBean(id = 0, name = name, unitName = unitName)
                DatabaseManager.getInstance(Utils.getApp())
                    .userInfoDao()
                    .deleteAllUsers()
                DatabaseManager.getInstance(Utils.getApp())
                    .userInfoDao()
                    .insertUsers(userInfoBean)
                mRootView?.loginSuccess()
            }
        }
    }
}
